<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" :menuName="menuName" @formSubmit="formSubmit">
		<om-autoform ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		errorModal,
		ignoreHtml,
		getUrl,
		confirmModal,
		ptFloat,
		minusF
	} from '@/static/js/common.js';
	import {
		changeConfigAttr,
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		hasEditor,
		getEditorTitle,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildFormData({})
		},
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		methods:{
			initLoad(options){
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code)
				]
				if(fileId){
					ajaxArr.push({
						url:"tpAward/getValue",
						param:{
							fileId:fileId
						}
					})
				}
				formModelLoad(ajaxArr,function(res,mainData){
					changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posname'), mainData);
					_that.$nextTick(function(){
						// _that.$refs.autoform.initZposId()
						if(res[0].length == 1){
						 	_that.$refs.autoform.initSelectIndex('topinfo', 'zposId')
						 	_that.$set(_that.formdata, 'zposId', res[0].id)
						 	_that.$set(_that.formdata, 'zposName', res[0].posname)
						}
					})
					if(_that.subEditorObj["title2"]){
						let att = {}
						let configData = _that.subConfigData['title2']
						if(hasEditor("title2",'awardId',configData)){
							att.titleTemplate =  "<%= awardName %>"
						}
						if(hasEditor("title2",'itemId',configData)){
							let title = getEditorTitle("title2",'itemId',configData)
							att.subtitle1Template =  title+":<%= itemName %>"
						}
						if(hasEditor("title2",'awardTypeName',configData)){
							let title = getEditorTitle("title2",'awardTypeName',configData)
							att.subtitle2Template =  title+":<%= awardTypeName %>"
						}
						changeEditor("title2","dataStr",att,mainData)
					}
					if(fileId) {
						var fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for (var i in res[1].value) {
							if (fieldArr.includes(i)) {
								_that.$refs.autoform.$set(_that.$refs.autoform.formdata, i, res[1].value[i])
								_that.$set(_that.formdata, i, res[1].value[i])
							}
						}
						var dataList = _that._.cloneDeep(res[1].value.dataList)
						_that.subEditorObj["title2"].workList = dataList
						_that.$refs.autoform.$set(_that.$refs.autoform.formdata, "dataStr", JSON.stringify(dataList))
						_that.$set(_that.formdata, "dataStr", JSON.stringify(dataList))
						delete res[1].value.dataList
						if (_that.subEditorObj["title2"]) { //配置子表的新增和删除
							changeEditor("title2", "dataStr", {
								showDel: !_that.checkedit, 
								showAdd: !_that.checkedit
							}, null, mainData)
						}
						_that.$nextTick(function(){
							_that.$refs.autoform.changeEditor("topinfo","zposId","type","labelbox")
						})
					}
					_that.$nextTick(function(){
						if(hasEditor("topinfo","zposId",mainData)){
							if(!_that.$refs.autoform.formdata.zposId){
								_that.$refs.autoform.initSelectVal("topinfo","zposId")
							}
						}
					})
				},options)
			},
			formSubmit() {
				let _that = this;
				if(_that.edit){
					_that.om.formSave('tpAward/doUpdate',_that.formdata,_that)
				}else{
					_that.om.formSave('tpAward/doAdd',_that.formdata,_that)
				}
			}
		},
		onLoad(options) {
			let _that = this
			_that.initLoad(options)
		},
		onShow() {
			let _that = this
			for (var fieldId in _that.subEditorObj) {
				var editor = _that.subEditorObj[fieldId];
				uni.getStorage({
					key: getWorkCacheKey(fieldId),
					success: function(res) {
						for(let item of res.data){
							if(item.fileList ==""){
								item.fileList = []
							}
						}
						editor.workList = res.data;
						uni.removeStorage({
							key: getWorkCacheKey(fieldId)
						});
					}
				});
			}
		}
	}
</script>
<style></style>                                                                                                                                                                                                                                                                                                                  
